package com.cyys.modules.core.utils;

import freemarker.template.Configuration;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

public class FreemarketConfigUtil {
    private static String path = new File(FreemarketConfigUtil.class.getClassLoader().getResource("templates").getFile()).getPath();
    public final static int TYPE_MAPPER = 1;
    public final static int TYPE_MODEL = 2;
    public final static int TYPE_DAO = 3;
    public final static int TYPE_SERVICE = 4;
    public final static int TYPE_ACTION = 5;
    public final static int PAGE_LIST = 6;
    public final static int PAGE_INPUT = 7;
    public final static int PAGE_SELECT = 8;
    private static Configuration configuration;

    public static Configuration getGeneralInstance() {
        if (null == configuration) {
            synchronized (FreemarketConfigUtil.class) {
                if (null == configuration) {
                    configuration = new Configuration(Configuration.VERSION_2_3_30);
                    try {
                        if (path.contains("jar")) {
                            configuration.setClassForTemplateLoading(FreemarketConfigUtil.class, "/templates/general");
                        } else {
                            configuration.setDirectoryForTemplateLoading(new File(path + File.separator + "general"));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    configuration.setEncoding(Locale.CHINA, "utf-8");
                }
            }
        }
        return configuration;
    }

    public static Configuration getTreeInstance() {
        if (null == configuration) {
            synchronized (FreemarketConfigUtil.class) {
                if (null == configuration) {
                    configuration = new Configuration(Configuration.VERSION_2_3_30);
                    try {
                        if (path.contains("jar")) {
                            configuration.setClassForTemplateLoading(FreemarketConfigUtil.class, "/templates/tree");
                        } else {
                            configuration.setDirectoryForTemplateLoading(new File(path+ File.separator + "tree"));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    configuration.setEncoding(Locale.CHINA, "utf-8");
                }
            }
        }
        return configuration;
    }
}
